﻿using RMS.EF.Model;
using RMS.EF.Model.Common;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;

namespace RMS.BLL
{
    public class ProgramBll
    {
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">分页参数</param>
        /// <param name="searchValue">搜索框</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public List<RMS_Program> GetByPage(PageParams page, string searchValue, E_Status status)
        {
            List<RMS_Program> result = new List<RMS_Program>();
            using (var db = new RMSEntities())
            {
                var q = db.RMS_Program.Where(p => p.Deleted == (int)E_Deleted.Normal);
                #region where
                if (!string.IsNullOrWhiteSpace(searchValue))
                {
                    q = q.Where(p => p.Name.Contains(searchValue) || p.Domain.Contains(searchValue));
                }
                if (status != E_Status.All)
                {
                    q = q.Where(p => p.Status == (int)status);
                }
                #endregion
                page.Total = q.Count();
                #region order by
                if (string.IsNullOrWhiteSpace(page.Order))
                {
                    q = q.OrderBy(p => p.Ordering);
                }
                else
                {
                    var orderExpression = string.Format("{0} {1}", page.Order, page.OrderType);
                    q = q.OrderBy(orderExpression);
                }
                #endregion
                q = q.Skip(page.Start).Take(page.Length);
                result = q.ToList();
            }
            return result;
        }
        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id">数据Id字符串","分隔</param>
        /// <returns></returns>
        public CommonOutPutVm Delete(string id)
        {
            CommonOutPutVm result = new CommonOutPutVm();
            using (var db = new RMSEntities())
            {
                db.Database.ExecuteSqlCommand("update RMS_Program set Deleted=1 where Id in (" + id + ")");
                db.SaveChanges();
            }
            result.IsSuccess = true;
            result.Message = "Delete success";
            return result;
        }
        /// <summary>
        /// 根据Id获取未删除的实体
        /// </summary>
        /// <param name="id">Id主键</param>
        /// <returns></returns>
        public RMS_Program GetById(string id)
        {
            RMS_Program result = new RMS_Program();
            using (var db = new RMSEntities())
            {
                result = db.RMS_Program.AsNoTracking().FirstOrDefault(p => p.Id == id && p.Deleted == (int)E_Deleted.Normal);
            }
            return result;
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model">编辑后的模型实体</param>
        /// <returns></returns>
        public CommonOutPutVm Edit(RMS_Program model)
        {
            CommonOutPutVm result = new CommonOutPutVm();
            using (var db = new RMSEntities())
            {
                bool exist = db.RMS_Program.Any(
                    p => p.Deleted == (int)E_Deleted.Normal 
                    && p.Id != model.Id 
                    && (p.Name == model.Name || p.Domain == model.Domain));
                if (exist)
                    throw new Exception("Name or domain already exists");
                db.RMS_Program.Attach(model);
                db.Entry(model).State =!string.IsNullOrWhiteSpace(model.Id) ? EntityState.Modified : EntityState.Added;
                int row = db.SaveChanges();
                if (row != 1)
                    throw new Exception("Save failed");
                result.IsSuccess = true;
                result.Message = "Save success";
            }
            return result;
        }
        /// <summary>
        /// 获取所有可用数据
        /// </summary>
        /// <returns></returns>
        public List<RMS_Program> GetAllEnable()
        {
            List<RMS_Program> result = new List<RMS_Program>();
            using (var db = new RMSEntities())
            {
                var q = db.RMS_Program.Where(
                    p => p.Deleted == (int)E_Deleted.Normal 
                    && p.Status == (int)E_Status.Enable)
                    .OrderBy(p => p.Ordering);
                result = q.ToList();
            }
            return result;
        }
    }
}
